package com.ruoyi.web.controller.dongfang;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.json.JSON;
import com.ruoyi.common.json.JSONObject;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.question.domain.Article;
import com.ruoyi.question.service.IArticleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 文章 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-10-11
 */
@Controller
@RequestMapping("/question/article")
public class ArticleController extends BaseController
{
    private String prefix = "question/article";
	
	@Autowired
	private IArticleService articleService;
	
	@RequiresPermissions("question:article:view")
	@GetMapping()
	public String article()
	{
	    return prefix + "/article";
	}
	
	/**
	 * 查询文章列表
	 */
	@RequiresPermissions("question:article:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Article article)
	{
		startPage();
        List<Article> list = articleService.selectArticleList(article);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出文章列表
	 */
	@RequiresPermissions("question:article:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Article article)
    {
    	List<Article> list = articleService.selectArticleList(article);
        ExcelUtil<Article> util = new ExcelUtil<Article>(Article.class);
        return util.exportExcel(list, "article");
    }
	
	/**
	 * 新增文章
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存文章
	 */
	@RequiresPermissions("question:article:add")
	@Log(title = "文章", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Article article)
	{
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String dateStr = sdf.format(date );
		article.setApproveStatus(0);
		article.setArticleCreateTime(dateStr);
		articleService.insertArticle(article);
		return AjaxResult.success(article.getId());
	}

	/**
	 * 修改文章
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap)
	{
		Article article = articleService.selectArticleById(id);
		mmap.put("article", article);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存文章
	 */
	@RequiresPermissions("question:article:edit")
	@Log(title = "文章", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Article article)
	{
		article.setApproveStatus(0);
		return toAjax(articleService.updateArticle(article));
	}
	
	/**
	 * 删除文章
	 */
	@RequiresPermissions("question:article:remove")
	@Log(title = "文章", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(articleService.deleteArticleByIds(ids));
	}

    //新增文件
    @Log(title = "新增文件", businessType = BusinessType.INSERT)
    @PostMapping("/saveFile")
    @ResponseBody
    public AjaxResult saveFile(HttpServletRequest request) throws Exception {
        Long articleId = Long.parseLong(request.getParameter("articleId"));
        String files = request.getParameter("array");
        JSONObject.JSONArray jsonArray =  JSON.unmarshal(files, JSONObject.JSONArray.class);
        if (null!= jsonArray && jsonArray.size()>0){
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject =(JSONObject)jsonArray.get(i);
                //Article article = new Article();
                //article.setId(articleId);
                Article newArticle = articleService.selectArticleById(articleId);
                //dfFile.setQuestionId(questionId);
                //dfFile.setFileId(jsonObject.getStr("fileId"));
                //dfFile.setFileName(jsonObject.getStr("fileName"));
                //dfFile.setFileType(jsonObject.getStr("fileType"));
                //dfFile.setFileSource(jsonObject.getStr("fileSource"));
                //fFile.setFileSource("1");
                //dfFile.setCreatetime(new Date());
				String url = "http://211.149.205.33:18080/file/down/";
                newArticle.setImageUrl(url+jsonObject.getStr("fileId"));
                articleService.updateArticle(newArticle);
            }
        }
        return AjaxResult.success();
    }
}
